﻿#pragma once
#include <iostream>
using namespace std;

//颜色-枚举值表⽰颜⾊
enum Colour
{
	RED,  //红
	BLACK //黑
};

//红黑树节点
template<class K,class V>
struct RBtreeNode
{
	pair<K, V> _kv;//存放数据
	RBtreeNode<K, V>* _left;  //左节点
	RBtreeNode<K, V>* _right; //右节点
	RBtreeNode<K, V>* _parent;//父亲节点

	Colour _col;//控制黑红颜色
	
	RBtreeNode(const pair<K,V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{ }
};

template<class K,class V>
class RBtree
{
	typedef RBtreeNode<K, V> Node;
public:
	//插入节点
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			//规则2，根结点是黑色的
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		//插入红色节点
		cur = new Node(kv);
		cur->_col = RED;
		if (kv.first < parent->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		 
		//变色
		while (parent && parent->_col == RED)
		{
			//爷爷节点
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				//   g
				// p   u
				//
				//叔叔节点
				Node* uncle = grandfather->_right;

				// uncle存在且为红
				if (uncle && uncle->_col == RED)
				{

					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续向上更新
					cur = grandfather;
					parent = cur->_parent;
				}
				else // 叔叔不存在且为黑色
				{
					//    g
					//  p   
					// c
					
					//插入在父亲的左边，右旋处理
					if (parent->_left == cur)
					{
						RotR(grandfather);
						grandfather->_col = RED;
						parent->_col = BLACK;
					}
					else //插入在父亲的右边，左右双旋处理
					{
						//    g
						//  p   
						//   c
						RotL(parent);
						RotR(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;
					}
					break;
				}
				
			}
			else  //if (grandfather->_right == parent) 
			{
				//爷爷的右节点是父亲，说明左节点是叔叔

				//    g
				//  u   p   
				//       c
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;
					//继续向上变色
					cur = grandfather;
					parent = cur->_parent;
				}
				else // 叔叔不存在且为黑色
				{
					//    g
					//      p   
					//       c

					//cur插入在右边，左旋处理
					if (parent->_right == cur)
					{
						RotL(grandfather);

						grandfather->_col = RED;
						parent->_col = BLACK;
					}
					else //cur插入在p的左边，右左双旋处理
					{
						//    g
						//      p   
						//    c

						RotR(parent);
						RotL(grandfather);
						grandfather->_col = RED;
						cur->_col = BLACK;

					}
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;

	}
	//查询
	bool Find(const K& kv)
	{
		Node* cur = _root;
		while (cur)
		{
			if (kv < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else if(kv > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了 
			cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}

		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了 
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}

	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;

		// 参考值 
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}



	void print()
	{
		_InOrder(_root);
	}

protected:
	//右旋转
	void RotR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		//subLR节点不为空，连接父亲节点
		if (subLR != nullptr)
		{
			subLR->_parent = parent;
		}
		//parent上面有可能还有节点，保存parent的父亲节点
		Node* ppNode = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		//如果ppNode为空，就说明parent之前为根节点
		if (ppNode == nullptr)
		{
			//更新根节点
			_root = subL;
			//父亲节点指向空
			subL->_parent = nullptr;
		}
		else //不为空，说明还有节点
		{
			//判断ppNode连接左边还是右边
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}
			//ppNode给subL的父亲节点
			subL->_parent = ppNode;

		}
	}
	//左旋转
	void RotL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		//subRL节点不为空，连接父亲节点
		if (subRL != nullptr)
		{
			subRL->_parent = parent;
		}
		//parent上面有可能还有节点，保存parent的父亲节点
		Node* ppNode = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		//如果ppNode为空，就说明parent之前为根节点
		if (ppNode == nullptr)
		{
			//更新根节点
			_root = subR;
			//父亲节点指向空
			subR->_parent = nullptr;
		}
		else // //不为空，说明还有节点
		{
			//判断ppNode连接左边还是右边
			if (ppNode->_left == parent)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}
			//subL的父亲节点连接ppNode。
			subR->_parent = ppNode;
		}

	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		_InOrder(root->_right);
	}
private:
	Node* _root = nullptr;
};